<?php
namespace jy_weishop\activity;
defined('BY_JYA') or exit('error');
/*
	重复购
*/
class buyagain extends \plugin{
	public function get_active($goods_id,$active_id,$optionid=''){
		$active=$this->get('activity_buyagain',array('id'=>$active_id));
		if(!$active){
			return false;
		}
		if($active['stop'] || $active['start_time']>TIMESTAMP || $active['end_time']<TIMESTAMP){
			return false;
		}
		//是否第二次购买
		$isbuy=$this->getcolumn('order_goods',array('goodsid'=>$goods_id,'ispay'=>1),'id');
		if(!$isbuy){
			return false;
		}
		if($active['start_time']<TIMESTAMP && $active['end_time']>TIMESTAMP){
			$status=1;
			$rest_time=$this->model()->date($active['end_time']-TIMESTAMP);
			$rest_time2=$active['end_time'];
		}
		if($active['start_time']>TIMESTAMP){
			$status=0;
			$rest_time=$this->model()->date($active['start_time']-TIMESTAMP);
			$rest_time2=$active['start_time'];
		}
		$goods_price=$this->getcolumn('goods',array('id'=>$goods_id),'price');
		if($optionid){
			$optionprice=pdo_getcolumn('jy_weishop_goods_option',array('goodsid'=>$goods_id,'id'=>$optionid),'price');
			$optionprice=price($optionprice*$active['discount']/10,true);
		}
		$active_price=price($goods_price*$active['discount']/10,true);
		return array(
			'title'=>'重复购优惠',
			'price'=>$optionprice?:$active_price,
			'optionprice'=>$optionprice,
			'end_time'=>date('Y-m-d H:i:s',$active['end_time']),
			'start_time'=>date('Y-m-d H:i:s',$active['start_time']),
			'status'=>$status,
			'rest_time'=>$rest_time,
			'rest_time2'=>$rest_time2,
			'nopercent'=>1
		);
	}
	public function check_stock($goods_id,$active_id,$num){
		return true;
	}
	public function buy($goods_id,$active_id,$num){
		$active=$this->get('activity_buyagain',array('id'=>$active_id));
		if(!$active){
			return false;
		}
		if($active['start_time']>TIMESTAMP || $active['end_time']<TIMESTAMP){
			return false;
		}
		if($active['num']<$num){
			exi('该活动每人只能购买'.$active['num'].'件商品！','error');
		}
		return true;
	}
	//$goods_id,$active_id,$num,$ordersn
	public function order_create($params){
		$log=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$params['mid']?:$_SESSION['uid'],
			'goods_id'=>$params['goods_id'],
			'active_id'=>$params['active_id'],
			'ordersn'=>$params['ordersn'],
			'num'=>$params['num'],
			'price'=>$params['goods_price'],
			'createtime'=>TIMESTAMP,
		);
		$this->insert('activity_buyagain_log',$log);
	}
	public function ispay($params){
		$params=array(
			'uniacid'=>$_SESSION['uniacid'],
			'ordersn'=>$params['ordersn'],
		);
		$this->update('activity_buyagain_log',array('ispay'=>1),$params);
	}
	public function task(){
		$lasttime=time()-600;
		$logs = pdo_getall('jy_weishop_activity_buyagain_log',array('uniacid'=>$_SESSION['uniacid'],'createtime >'=>$lasttime));
		foreach($logs as $item){
			$status=pdo_getcolumn('jy_weishop_order',array('ordersn'=>$item['ordersn'],'uniacid'=>$_SESSION['uniacid']),'status');
			if($status==0 && $item['createtime']+120<time()){
				pdo_delete('jy_weishop_activity_buyagain_log',array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
}